﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_UserAccountManager : System.Web.UI.Page
{
    private UserAccountBlo _UABlo = new UserAccountBlo();
    private MobileBlo _MobileBlo = new MobileBlo();
    int id_UA_tmp;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //lblStatus.Visible = false;
            LoadUserAccount();
            //LoadgdvMobileNumber(2);
        }
    }
    
    private void LoadUserAccount()
    {
        string _searchColumn = ddlUserAccount.SelectedValue.ToString();
        string _searchString;
        _searchString = txtSearchUserAccount.Text.ToString();
        gdvUserAccount.DataSource = _UABlo.SelectUserAccountBy(_searchColumn, _searchString);
        gdvUserAccount.DataBind();
    }
    //protected void gdvSearchByUserAccount_RowUpdating(object sender, GridViewUpdateEventArgs e)
    //{

    //    int id_UA = int.Parse(gdvUserAccount.DataKeys[e.RowIndex].Value.ToString());
    //    gdvMobileNumber.DataSource = this.SelectMobileByID_UA(id_UA);
    //    gdvMobileNumber.DataBind();
    //}
    private void LoadgdvMobileNumber(int id_UA)
    {
        gdvMobileNumber.DataSource = _MobileBlo.SelectMobileByID_UA(id_UA);;
        gdvMobileNumber.DataBind();

    }

    protected void gdvSearchByUserAccount_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvUserAccount.PageIndex = e.NewPageIndex;
        LoadUserAccount();
    }
    protected void btnShowAllUserAccount_Click(object sender, EventArgs e)
    {
        txtSearchUserAccount.Text = "";
        LoadUserAccount();
        
    }
    protected void btnSearchUserAccount_Click(object sender, EventArgs e)
    {
        LoadUserAccount();
    }
    protected void gdvMobileNumber_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id_MN = int.Parse(gdvMobileNumber.DataKeys[e.RowIndex].Value.ToString());
        Response.Redirect("Bill.aspx");     //Truyen ID_MN vao session de goi ra bill.aspx        
    }

    protected void gdvSearchByUserAccount_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gdvUserAccount.EditIndex = e.NewEditIndex;
        LoadUserAccount();
    }
    protected void gdvUserAccount_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gdvUserAccount.EditIndex = -1;
        LoadUserAccount();
    }
    protected void gdvUserAccount_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            int id_UA = int.Parse(gdvUserAccount.DataKeys[e.RowIndex].Value.ToString());            
            string _fullName = ((TextBox)gdvUserAccount.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            string _email = ((TextBox)gdvUserAccount.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
            string _adress = ((TextBox)gdvUserAccount.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
            bool _sex=false;
            if (((DropDownList)gdvUserAccount.Rows[e.RowIndex].Cells[3].FindControl("ddlEditSex")).SelectedValue.ToString()=="1")
            {
                _sex=true;
            }
            string _birthday = (DateTime.ParseExact(((TextBox)gdvUserAccount.Rows[e.RowIndex].Cells[2].Controls[0]).Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yyyy"));
            UserAccountDto _userAccount = new UserAccountDto(id_UA,_fullName,_birthday,_sex,_email,_adress);
            _UABlo.UpdateUserAccount(_userAccount);

            }
            catch (Exception)
            {
                lblStatus.Visible = true;
                lblStatus.Text = " Your data is not valid! Please try again";            
                gdvUserAccount.EditIndex = e.RowIndex;
                LoadUserAccount();
                return;
            }
        gdvUserAccount.EditIndex = -1;
        LoadUserAccount();
        lblStatus.Visible = true;
        lblStatus.Text = "Update successfull ";
    }
    protected void gdvUserAccount_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //lblStatus.Visible = true;
        //lblStatus.Text = "sgsfg";
        string strCommandName = e.CommandName.ToString();
        if (strCommandName=="cmdSelectMobile")
        {
            ImageButton ibtn = (ImageButton)e.CommandSource;
            int cmdindex = Convert.ToInt32(ibtn.CommandArgument.ToString());
            int id_UA = int.Parse(gdvUserAccount.DataKeys[cmdindex].Value.ToString());
            this.id_UA_tmp = id_UA;
            Response.Redirect("#");                     // goi managerMobileNumber.aspx (truyen theo mot thang id_UA)
            lblStatus.Text = this.id_UA_tmp.ToString();
        }
    }
    protected void gdvMobileNumber_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
        gdvMobileNumber.EditIndex =e.NewEditIndex;
        lblStatus.Text = id_UA_tmp.ToString();
        LoadgdvMobileNumber(id_UA_tmp);
    }
    protected void gdvMobileNumber_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gdvMobileNumber.EditIndex = -1;
        LoadgdvMobileNumber(id_UA_tmp);
    }
    protected void gdvUserAccount_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id_UA = int.Parse(gdvUserAccount.DataKeys[e.RowIndex].Value.ToString());
        _UABlo.DeleteUserAccount(id_UA);
        lblStatus.Text = "Delete Successfull";
        LoadUserAccount();
    }
}